﻿using B.S.DataBase.MyProject.ErrorCode;
using B.S.Traceability.Domain;
using B.S.Traceability.Infrastructure.Interface;
using B.S.Traceability.Write.API.Applicantion.Command.PhoneCommand;
using MediatR;

namespace B.S.Traceability.Write.API.Applicantion.CommandHandler.PhoneCommandHandler
{
    public class DelPhoneCommandHandler : IRequestHandler<DelPhoneCommand, APIResult<int>>
    {
        private readonly IBaseRepository<SU_PhoneInfo> phoneRep;
        public DelPhoneCommandHandler(IBaseRepository<SU_PhoneInfo> phoneRep)
        {
            this.phoneRep = phoneRep;
        }
        public Task<APIResult<int>> Handle(DelPhoneCommand request, CancellationToken cancellationToken)
        {
            APIResult<int> result=new APIResult<int>();
            var list= phoneRep.GetAll().Result.Where(x=>x.PhoneId==request.PhoneId).FirstOrDefault();
            var res = phoneRep.Delete(list).Result;
            if (res>0)
            {
                result.Code = APIEnum.删除成功;
                result.Msg = APIEnum.删除成功.ToString();
                result.data = res;
            }
            else
            {
                result.Code = APIEnum.删除失败;
                result.Msg = APIEnum.删除失败.ToString();
                result.data = res;
            }
            return Task.FromResult(result);
        }
    }
}
